﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using BusinessObjects.BO_Actions.EBooks;
using Entity.StoreProcedure;
using LibraryO2S;
using System.Collections.Generic;

public partial class controls_admin_EBooks : Pages_Base
{
    protected int m_id;
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["m_id"] != null)
            m_id = Convert.ToInt32(Request["m_id"]);
        else m_id = -1;
        base.removeHGC(divMessage);
        base.removeHGC(divResult);
        if (!IsPostBack)
        {
            try
            {
                base.loadCategories(ddlCategory, 3);
                if (Request["c_id"] != null)
                    ddlCategory.SelectedValue = Request["c_id"].ToString();
                loadData();
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Có lỗi khi truy cập hệ thống! <br>" + ex.Message);
            }
            finally { base.CloseConnection(); }
        }
    }
    protected void loadData()
    {
        try
        {
            string keyword = String.Empty;
            int c_id = -1;
            if (Request["k_w"] != null)
            {
                keyword = Request["k_w"].ToString();
                c_id = Convert.ToInt32(ddlCategory.SelectedValue);
                update.Visible = false;
            }
            else if (m_id == -1 || (m_id != -1 && Request["t"] == null))
            {
                c_id = Convert.ToInt32(ddlCategory.SelectedValue);
                update.Visible = false;
            }
            objDT = BO_EBooks.SelectAdmin(base.Cur_page, base.Page_size, m_id, keyword, c_id);
            if (!bindRepeater(rptNews, objDT, pagerContainer, divMessage))
                if (Request["k_w"] != null)
                    divMessage.InnerHtml = O2S_Message.Warning("Hệ Thống", "Không có bản ghi nào phù hợp với từ khóa: " + Request["k_w"].ToString() + "!");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void rptNews_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            base.OpenConnection();
            HiddenField hdfID = (HiddenField)e.Item.FindControl("hdfID");
            int e_id = Convert.ToInt32(hdfID.Value);
            string commandName = e.CommandName;
            if (commandName.Equals("del"))
            {
                BO_EBooks.Delete(e_id);
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Xóa thành công .");
            }
            else if (commandName.Equals("Display"))
                if (Request["t"] == null) BO_EBooks.Display(e_id, -1);
                else BO_EBooks.Display(e_id, m_id);
            loadData();
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message);
        }
        finally { base.CloseConnection(); }
    }
    protected void rptNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (Request["t"] == null)
        {
            Button btnDisplay = (Button)e.Item.FindControl("btnDisplay");
            if (btnDisplay != null)
                if (!Convert.ToBoolean(btnDisplay.CommandArgument))
                    btnDisplay.CssClass = "icon-32-cancel";
                else
                    btnDisplay.CssClass = "icon-32-checking";
        }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?module=ebookadd");
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string keyword = txtSearch.Value.Trim();
        if (keyword != null && !keyword.Equals("") && !keyword.Equals("Hãy nhập từ khóa !"))
        {
            string url = base.removeRequest();
            Response.Redirect(url + "&k_w=" + keyword);
        }
    }
    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        string url = base.removeRequest();
        if (Request["c_id"] != null)
        {
            int conti = url.LastIndexOf("c_id=");
            string start = url.Substring(0, url.LastIndexOf("&c_id="));
            string end = url.Substring(conti, url.Length - conti);
            conti = end.IndexOf("&");
            if (conti > -1)
                url = start + end.Substring(conti, end.Length - conti);
            else
                url = start;
        }
        Response.Redirect(url + "&c_id=" + ddlCategory.SelectedValue);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        string del_list = hdfCheck.Value;
        string[] sp_list = del_list.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        BOEBooks_Delete _action = new BOEBooks_Delete();
        usp_EBooks_Delete _entity;
        List<usp_EBooks_Delete> _listData = new List<usp_EBooks_Delete>();
        foreach (string per in sp_list)
        {
            _entity = new usp_EBooks_Delete();
            _entity.EBOOK_ID = Convert.ToInt32(per);
            _listData.Add(_entity);
        }
        if (_action.ExecuteListDataAndReturnBooleanValue(_listData))
            divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Thực hiện thành công.");
        else
            divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Chưa thực hiện được !");
        loadData();
    }
}
